//
//  SSColor.swift
//  BBetter
//
//  Created by Gmc on 2020/3/1.
//  Copyright © 2020 Gmc. All rights reserved.
//

import UIKit

extension UIColor {
    public class func color(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) -> UIColor {
        let redF = red / 255
        let greenF = green / 255
        let blueF = blue / 255
        let alphaF = alpha / 1.0
        let colorspace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
        let components:[CGFloat] = [redF, greenF, blueF, alphaF]
        let color = CGColor(colorSpace: colorspace, components: components)!
        let resultColor = UIColor(cgColor: color)
        return resultColor
    }
    
    public class func colorFromRGB(rgbValue: Int, alpha: CGFloat) -> UIColor {
        return UIColor(red: ((CGFloat)((rgbValue & 0xFF0000) >> 16))/255.0, green: ((CGFloat)((rgbValue & 0xFF00) >> 8))/255.0, blue: ((CGFloat)(rgbValue & 0xFF))/255.0, alpha: alpha)
    }
    
    public class func colorFromRGB(rgbValue: Int) -> UIColor {
        return UIColor(red: ((CGFloat)((rgbValue & 0xFF0000) >> 16))/255.0, green: ((CGFloat)((rgbValue & 0xFF00) >> 8))/255.0, blue: ((CGFloat)(rgbValue & 0xFF))/255.0, alpha: 1.0)
    }
    
    public class func colorWithHexString(hexString: String) -> UIColor {
        return UIColor.colorWithHexString(hexString: hexString, alpha: 1.0)
    }
    
    public class func colorWithHexString(hexString: String, alpha: CGFloat) -> UIColor {
        var cString:String = hexString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
        if cString.count < 6 {
            return .black
        }
        if cString.hasPrefix("0X") {
            let s = cString.dropFirst(2)
            cString = String(s)
//            let start = cString.index(cString.startIndex, offsetBy: 2)
//            let s = cString[start ..< cString.endIndex]
//            cString = String(s)
        }
        if cString.hasPrefix("#") {
//            let start = cString.index(cString.startIndex, offsetBy: 1)
//            let s = cString[start ..< cString.endIndex]
//            cString = String(s)
            let s = cString.dropFirst(1)
            cString = String(s)
        }
        
        if cString.count != 6 {
            return .black
        }
        
        let rString = cString[cString.startIndex ..< cString.index(cString.startIndex, offsetBy: 2)]
        let gString = cString[cString.index(cString.startIndex, offsetBy: 2) ..< cString.index(cString.startIndex, offsetBy: 4)]
        let bString = cString[cString.index(cString.startIndex, offsetBy: 4) ..< cString.endIndex]
        var r:UInt64 = 0
        var g:UInt64 = 0
        var b:UInt64 = 0
        Scanner(string: String(rString)).scanHexInt64(&r)
        Scanner(string: String(gString)).scanHexInt64(&g)
        Scanner(string: String(bString)).scanHexInt64(&b)
        return self.init(red: CGFloat(Double(r) / 255.0), green: CGFloat(Double(g) / 255.0), blue: CGFloat(Double(b) / 255.0), alpha: alpha)
    }
    
    static func random() -> UIColor {
        return color(red: arc4random_uniform(256), green: arc4random_uniform(256), blue: arc4random_uniform(256))
    }
    
    static func color(red: UInt32, green: UInt32, blue: UInt32) -> UIColor {
        return UIColor(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
    }
    
    //2020-04-04 为在抗击新冠状病毒所牺牲的英雄致敬, 默哀
    static func sacrificeColor() -> UIColor {
        return UIColor(red:39.0 / 255.0, green: 39.0 / 255.0, blue: 39.0 / 255.0, alpha: 1.0)
    }

}
